/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
module dwt.dnd.texttransfer;


private import dwt.dwt;

private import dwt.dnd.bytearraytransfer;
private import dwt.dnd.transferdata;
private import dwt.internal.ole.win32.com;
private import dwt.internal.ole.win32.OBJIDL;


/**
 * The class <code>TextTransfer</code> provides a platform specific mechanism 
 * for converting plain text represented as a java <code>String</code> 
 * to a platform specific representation of the data and vice versa.  See 
 * <code>Transfer</code> for additional information.
 * 
 * <p>An example of a java <code>String</code> containing plain text is shown 
 * below:</p>
 * 
 * <code><pre>
 *     String textData = "Hello World";
 * </code></pre>
 */
public class TextTransfer : ByteArrayTransfer {

	private static TextTransfer _instance;
	private static char[] CF_UNICODETEXT = "CF_UNICODETEXT";
	private static char[] CF_TEXT = "CF_TEXT";
	private static const int CF_UNICODETEXTID = COM.CF_UNICODETEXT;
	private static const int CF_TEXTID = COM.CF_TEXT;

	
private this() {
}

/**
 * Returns the singleton instance of the TextTransfer class.
 *
 * @return the singleton instance of the TextTransfer class
 */
public static TextTransfer getInstance () {
	if(_instance is null)
		_instance = new TextTransfer();
	return _instance;
}

/**
 * This implementation of <code>javaToNative</code> converts plain text
 * represented by a java <code>String</code> to a platform specific representation.
 * For additional information see <code>Transfer#javaToNative</code>.
 * 
 * @param object a java <code>String</code> containing text
 * @param transferData an empty <code>TransferData</code> object; this
 *  object will be filled in on return with the platform specific format of the data
 */
public void javaToNative (Object object, TransferData transferData){
	transferData.result = COM.E_FAIL;
	if (object is null) return;

	StringObj arbObj = cast(StringObj)object;
	if(arbObj is null || arbObj.data is null) return;
	
	char[] string = arbObj.data;
	if (string.length == 0) return;
	if (!isSupportedType(transferData)) {
		// did not match the TYMED
		transferData.stgmedium = new STGMEDIUM();
		transferData.result = COM.DV_E_TYMED;
		return;
	}
	switch (transferData.type) {
		case COM.CF_UNICODETEXT: {
			wchar[] buffer = Converter.StrToWCHARs(string);
			buffer ~= '\0';
			int byteCount = buffer.length * wchar.sizeof;
			HGLOBAL newPtr = COM.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, byteCount);
			COM.MoveMemory(newPtr, buffer.ptr, byteCount);						
			transferData.stgmedium = new STGMEDIUM();
			transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
			transferData.stgmedium.unionField = newPtr;
			transferData.stgmedium.pUnkForRelease = null;
			transferData.result = COM.S_OK;
			break;
		}
		case COM.CF_TEXT: {
			if (string.length == 0) {
				transferData.stgmedium = new STGMEDIUM();
				transferData.result = COM.DV_E_STGMEDIUM;
				return;
			}
			TCHAR[] buffer;
			buffer = Converter.StrToTCHARs(string);
			buffer ~= '\0';
			int cchMultiByte = buffer.length * TCHAR.sizeof;
			
			HGLOBAL lpMultiByteStr = COM.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, cchMultiByte);
			COM.MoveMemory(lpMultiByteStr, buffer.ptr, cchMultiByte);
			transferData.stgmedium = new STGMEDIUM();
			transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
			transferData.stgmedium.unionField = lpMultiByteStr;
			transferData.stgmedium.pUnkForRelease = null;
			transferData.result = COM.S_OK;
			break;
		}
		default : break;
	}
	return;
}

/**
 * This implementation of <code>nativeToJava</code> converts a platform specific 
 * representation of plain text to a java <code>String</code>.
 * For additional information see <code>Transfer#nativeToJava</code>.
 * 
 * @param transferData the platform specific representation of the data to be 
 * been converted
 * @return a java <code>String</code> containing text if the 
 * conversion was successful; otherwise null
 */
public Object nativeToJava(TransferData transferData){
	if (!isSupportedType(transferData) || transferData.pIDataObject is null) return null;
	
	IDataObject data = transferData.pIDataObject;
	data.AddRef();
	FORMATETC* formatetc = transferData.formatetc;
	STGMEDIUM* stgmedium = new STGMEDIUM();
	stgmedium.tymed = COM.TYMED_HGLOBAL;	
	transferData.result = data.GetData(formatetc, stgmedium);
	data.Release();
	if (transferData.result != COM.S_OK) return null;
	HGLOBAL hMem = cast(HGLOBAL)stgmedium.unionField;
	try {
		switch (transferData.type) {
			case CF_UNICODETEXTID: {
				/* Ensure byteCount is a multiple of 2 bytes on UNICODE platforms */
				void* lpMultiByteStr = COM.GlobalLock(hMem);
				if (lpMultiByteStr is null) return null;
				try {
					StringObj arbObj = new StringObj();
					arbObj.data = Converter.WCHARzToStr(cast(wchar*)lpMultiByteStr, -1);
					return arbObj;
				} finally {
					COM.GlobalUnlock(hMem);
				}
			}
			case CF_TEXTID: {
				
				void* lpMultiByteStr = COM.GlobalLock(hMem);
				if (lpMultiByteStr is null) return null;
				try {
					StringObj arbObj = new StringObj();
					arbObj.data = Converter.TCHARzToStr(cast(TCHAR*)lpMultiByteStr, -1);
					return arbObj;
				} finally {
					COM.GlobalUnlock(hMem);
				}
			}
			default : break;
		}
	} finally {
		// COM.GlobalFree(hMem);
		hMem = null;
	}
	return null;
}

protected int[] getTypeIds(){
	int[] intArr;
	intArr ~= CF_UNICODETEXTID;
	intArr ~= CF_TEXTID;
	return intArr;
}

protected char[][] getTypeNames(){
	char[][] StringArr;
	StringArr ~= (CF_UNICODETEXT);
	StringArr ~= (CF_TEXT);
	return StringArr;
}

boolean checkText(Object object) {
	return (object !is null  && cast(StringObj)object && (cast(StringObj)object).data.length > 0);
}


boolean validate(Object object) {
	return checkText(object);
}

}
